/**
 * created by 江心才子, 2020/3/18
 * Copyright (c) 2020, 270628297@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.wyl.jdbc

import com.wyl.JdbcUtil.executeQuery
import com.wyl.domain.Student
import java.sql.DriverManager

/**
 * 项目名称：LearnJava-JDBC
 * 创建人：江心才子
 * 创建时间：2020-03-18 11:41
 * 内容描述：
 *  定义一个方法，查询student表的数据将其封装为对象，然后装载集合，返回。
 * 修改说明：
 */
fun main() {
    findAllStudent2().forEach {
        println(it)
    }
}

fun findAllStudent2(): List<Student> {
    val sql = "SELECT * from student"
    val list = mutableListOf<Student>()

    executeQuery(sql) {
        while (it.next()) {
            list.add(Student(
                id = it.getInt("id"),
                name = it.getString("name"),
                age = it.getInt("age"),
                score = it.getFloat("score"),
                birthday = it.getDate("birthday"),
                insertTime = it.getString("insert_time")
            ))
        }
    }

    return list
}

fun findAllStudent(): List<Student> {
    val sql = "SELECT * from student"
    val list = mutableListOf<Student>()

    Class.forName("com.mysql.jdbc.Driver")
    DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/db3",
        "root",
        "root"
    ).use { conn ->
        conn.createStatement().use { stm ->
            stm.executeQuery(sql).use {
                while (it.next()) {
                    list.add(Student(
                        id = it.getInt("id"),
                        name = it.getString("name"),
                        age = it.getInt("age"),
                        score = it.getFloat("score"),
                        birthday = it.getDate("birthday"),
                        insertTime = it.getString("insert_time")
                    ))
                }
            }
        }
    }
    return list
}